//
//  ClassifyDetail.m
//  A段项目,漫画
//
//  Created by dllo on 15/4/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ClassifyDetail.h"

#import "MJRefresh.h"
#import "AFHTTPRequestOperation.h"
#import "AFHTTPRequestOperationManager.h"
#import "AFHTTPSessionManager.h"

#import "ClassifyModel.h"
#import "CartoonDViewController.h"

#import "ClassifyDetailCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#define HEIGHT  self.view.frame.size.height
#define WIDTH   self.view.frame.size.width


@interface ClassifyDetail ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,retain)NSMutableArray *myArr;
@property(nonatomic,retain)UICollectionView *classCollectionView;
@property(nonatomic,assign)NSInteger page;

@end

@implementation ClassifyDetail

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.myArr = [NSMutableArray array];
        self.page = 0;
    }
    return self;
}

  // 解析数据
- (void)classData
{
    self.page++;
    NSString *URL = [NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Classify/comicClassifyDetailForIos/local_version/2.3.2/channel/1001/guest_id/921199FA-97DD-457B-80FA-5A7E1D0FC7BA/uin/null/classify_id/%@/page/%ld/listcnt/12",self.classify_id,self.page];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:URL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *resDic = responseObject;
        NSMutableArray *arr = [resDic objectForKey:@"data"];
        for (NSMutableDictionary *classDic in arr) {
            ClassifyModel *classModel = [[ClassifyModel alloc] init];
            [classModel setValuesForKeysWithDictionary:classDic];
            [self.myArr addObject:classModel];
            [classModel release];
        }
        [self.classCollectionView reloadData];
        NSLog(@"数据请求成功class");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"数据请求失败class");
        
    }];
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor colorWithRed: 215 / 256.0 green:217 / 256.0 blue: 240 / 256.0 alpha:1.0];
    
    [self createview];
    [self classData];
    [self createfootView];
    
}


- (void)createview
{
    
    
    CGFloat bili1 = 107 / 80.0;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing =20;
    flowLayout.itemSize = CGSizeMake(WIDTH / 4.5, WIDTH / 4.5 * bili1 + 40);
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    
    self.classCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH - 0, HEIGHT - 64 - 49) collectionViewLayout:flowLayout];
    [self.view addSubview:self.classCollectionView];
    self.classCollectionView.backgroundColor =  [UIColor colorWithRed: 238 / 256.0 green:226 / 256.0 blue: 176 / 256.0 alpha:1.0];
    
    [_classCollectionView release];
    
    self.classCollectionView.delegate = self;
    self.classCollectionView.dataSource = self;
    
    [self.classCollectionView registerClass:[ClassifyDetailCollectionViewCell   class] forCellWithReuseIdentifier:@"reuse"];
  
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.myArr.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    ClassifyDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
    
    ClassifyModel *model = self.myArr[indexPath.row];
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_url]];
    
    
    cell.grade_aveLabel.text = model.grade_ave;
    cell.titleLabel.text = model.title;
    return cell;
}


   // 上拉加载
- (void)createfootView
{
    [self.classCollectionView addFooterWithCallback:^{
        
        [self classData];
        [self.classCollectionView reloadData];
        [self.classCollectionView footerEndRefreshing];
        
    }];
    
    [self.classCollectionView addHeaderWithCallback:^{
        NSLog(@"awdawdawdawd");
        self.page = 0;
        [self.myArr removeAllObjects];
        [self classData];
        [self.classCollectionView reloadData];
        [self.classCollectionView headerEndRefreshing];
    }];
}



 // 跳转


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CartoonDViewController *carToon = [[CartoonDViewController alloc] init];
    carToon.comic_id  = [self.myArr[indexPath.row] comic_id];
    [self.navigationController pushViewController:carToon animated:YES];
    

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
